

-- ac.game:event '单位-使用物品'(function(_,hero,item)
--     if item.item_class=='符文胚子' then
--         local player = hero.owner
--         local key = item.name
--         local odds = odds_list[1]
--         local lock = {}
--         local list = {}
--         while #list<3 do
--             local lv = table.random_table_index(odds)
--             local temp = player:random_item(key,lv)
--             local name = temp.name
--             if lock[name]==nil then
--                 lock[name] = true
--                 list[#list + 1] = ac.item.new(name)
--             end
--         end
--         list._unit = hero
--         item:remove()
--         player:notify('玩家-随机选择装备',player,list)
--     end
-- end)

local item_class = require 'types.item'.__index

local tags = {'S+','S_','A+','A_','B+','B_','C+','C_','D+','D_'}
--注册
local excel = {}
for _,data in ipairs(load_excel('符文装备.xlsx','洗练数值')) do
    local name = data.name
    if name then
        excel[#excel + 1] = data
    end
end


function item_class:copy_info()
    return {
        owner_player = self.owner_player,
        _xl_state = self._xl_state,
        _xl_tips = self._xl_tips,
    }
end

function item_class:xl_tip()
    local tip = self._xl_tips or ''
    if self['无法洗练'] then
        return ('|n=================|n%s|n|n'):format(tip)
    end
    if ac.enemy and ac.enemy.hard>1 then
        return ('|n=================|n点击洗练2000金币|n%s|n|n'):format(tip)
    end
    return ('|n=================|n难2开启洗练|n%s|n|n'):format(tip)
end

--[洗练文本介绍]
item_class._xl_tips = ''
function item_class:update_xl()
    local tips = {}
    local state = self._xl_state or {}
    table.sort(state,function(a,b)
        return a.level<b.level
    end)
    for a,da in ipairs(state) do
        tips[a] = da.tip
    end
    self._xl_tips = table.concat(tips,'|n')
    return tips
end


function item_class:ex_tip()
    local player = self.owner_player or ac.player.self
    return ('|cff999999所属玩家：|r%s'):format(player:get_name())
end

function item_class:random_entry(lock)
    local lock = lock or {}
    local entry = {}
    local max = #tags
    local index = math.ceil(math.sqrt(math.random(1,max^2)))
    local tag = tags[index]
    for _,data in ipairs(excel) do
        local key = data.name
        local num = data[tag]
        if lock[key]==nil and num then
            local tip = ('%s%s'):format(tag,sx_tip(key,num))
            entry[#entry + 1] = {key,num,tip = tip,level = index}
        end
    end
    local data = table.random(entry)
    return data
end

function item_class:init_entry()
    local level = self.lv or 0
    local num = self['随机词条'] or 0
    local state = self._xl_state or {}
    for a=1,num do
        if state[a]==nil then
            local data = self:random_entry()
            state[a] = data
        end
    end
    self._xl_state = state
    
    local tips = self:update_xl()
    
    if level<=5 then
        for a=num+1,2 do
            tips[a] = ('|cff999999随机词条(Lv%0.f解锁)|r'):format(a*2+1)
        end
        self._xl_tips = table.concat(tips,'|n')
    end
end


ac.game:event '物品-创建'(function(_,item)
    if item.item_class=='符文装备' then
        item:init_entry()
    end
end)

--[词条洗练]
ac.game:event '单位-点击物品'(function(_,hero,item)
    local player = hero.owner
    local level = item.lv
    if ac.enemy then
        --难2才能洗6级装备
        if level==6 and ac.enemy.hard<2 then
            return
        end
    end
    if item.item_class=='符文装备' and item.on_use==nil and item['无法洗练']==nil and level>=6 then
        local num = item.lv - 4
        local max = #tags
        local state = {}
        for a=1,num do
            local data = item:random_entry()
            state[a] = data
        end

        load_item_state(hero,item._xl_state,-1)
        load_item_state(hero,state,1)
        item._xl_state = state
        item:update_xl()
    end
end)



ac.game:event '单位-添加物品属性'(function(_,hero,item)
    load_item_state(hero,item._xl_state,1)
end)

ac.game:event '单位-移除物品属性'(function(_,hero,item)
    load_item_state(hero,item._xl_state,-1)
end)